//
//  RootViewController.m
//  Wetrip
//
//  Created by A5 on 14-9-15.
//  Copyright (c) 2014年 胡中磊. All rights reserved.
//

#import "RootViewController.h"
#import "BaseNavigationController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "FifthViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController

#pragma mark ---- life cycle

- (void)loadView
{
    [super loadView];
    
    [self _initTabBarView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tabBar setHidden:YES];
    
    [self _initViewControllers];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---- UITableViewDelegate

#pragma mark ---- CustomDelegate

#pragma mark ---- Event response

-(void)selectedTab:(UIButton *)sender
{
    btn1.selected = NO;
    btn2.selected = NO;
    btn3.selected = NO;
    btn4.selected = NO;
    btn5.selected = NO;
    
    sender.selected = YES;
    
    //索引通过tag值读取viewController
    self.selectedIndex = sender.tag - 1;
}

#pragma mark ---- Interface methods

-(void)setTabarHiend:(BOOL)animated
{
    [self.tabBar setHidden:YES];
    tabBarView.hidden=animated;
}

#pragma mark ---- private methods

// 私有的方法，方法名前面要加下划线
// 如果是初始化视图或数据方法名:_initXXXX
-(void)_initViewControllers
{
    FirstViewController *firstCtrl=[[FirstViewController alloc]init];
    
    SecondViewController *secondCtrl=[[SecondViewController alloc]init];
    
    ThirdViewController *thirdCtrl=[[ThirdViewController alloc]init];
    
    FourthViewController *fourthCtrl=[[FourthViewController alloc]init];
    
    FifthViewController *fifthCtrl=[[FifthViewController alloc]init];
    
    NSArray *viewCtrls=[[NSArray alloc]initWithObjects:firstCtrl,secondCtrl,thirdCtrl,fourthCtrl,fifthCtrl,nil];
    NSMutableArray *viewNavs=[NSMutableArray arrayWithCapacity:5];
    for (UIViewController *viewController in viewCtrls)
    {
        BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:viewController];
        [viewNavs addObject:nav];
    }
    self.viewControllers=viewNavs;
}
// 创建自定义Tabbar工具栏
-(void)_initTabBarView
{
    tabBarView=[[UIView alloc]initWithFrame:CGRectMake(0,ScreenBunds.size.height - 49, ScreenBunds.size.width,49)];
    tabBarView.backgroundColor=[UIColor whiteColor];
    tabBarView.layer.borderWidth = 0.5;
    tabBarView.layer.borderColor = [[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1.0] CGColor];
    [self.view addSubview:tabBarView];
    
    btn1 = [self getButtonWithTitle:@"第一" tag:1];
    btn1.selected = YES;
    
    btn2 = [self getButtonWithTitle:@"第二" tag:2];
    
    btn3 = [self getButtonWithTitle:@"第三" tag:3];
    
    btn4 = [self getButtonWithTitle:@"第四" tag:4];
    
    btn5 = [self getButtonWithTitle:@"第五" tag:5];
    
    [tabBarView addSubview:btn1];
    [tabBarView addSubview:btn2];
    [tabBarView addSubview:btn3];
    [tabBarView addSubview:btn4];
    [tabBarView addSubview:btn5];
}

#pragma mark ---- getters and setters

-(UIButton*)getButtonWithTitle:(NSString*)title tag:(NSInteger)tag
{
    NSInteger screenWidth = [[UIScreen mainScreen]bounds].size.width;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = tag;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    btn.frame = CGRectMake((tag - 1) * (screenWidth / 5.0), 0, screenWidth / 5.0, 49);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
    
}

@end
